using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WozLib.UI.Ink;
using System.ComponentModel;

namespace WozLib.UI.Controls
{
  /// <summary>
  /// Interaction logic for ToolSelectComboBox.xaml
  /// </summary>

  public partial class ToolSelectComboBox : System.Windows.Controls.ComboBox, INotifyPropertyChanged
  {


    public static readonly DependencyProperty UserCurrentInkToolProperty =
            DependencyProperty.Register("UserCurrentInkTool", typeof(SketchPad.InkTools), typeof(ToolSelectComboBox),
      new UIPropertyMetadata(SketchPad.InkTools.Selector, new PropertyChangedCallback(UserCurrentInkTool_Changed)));

    public SketchPad.InkTools UserCurrentInkTool
    {
      get { return (SketchPad.InkTools)GetValue(UserCurrentInkToolProperty); }
      set 
      {
        SetValue(UserCurrentInkToolProperty, value);
        NotifyPropertyChanged("UserCurrentInkTool");
      }
    }

    public static readonly DependencyProperty SketchPadProperty =
            DependencyProperty.Register("SketchPad", typeof(SketchPad), typeof(ToolSelectComboBox),
      new UIPropertyMetadata(new PropertyChangedCallback(SketchPad_Changed)));

    public SketchPad SketchPad
    {
      get { return (SketchPad)GetValue(SketchPadProperty); }
      set
      {
        SetValue(SketchPadProperty, value);
      }
    }

    //public List<SketchPad.InkTools> UserToolsEnabledCollection
    //{
    //  get { return _UserToolsEnabledCollection; }
    //  set { _UserToolsEnabledCollection = value; }
    //} private List<SketchPad.InkTools> _UserToolsEnabledCollection = new List<SketchPad.InkTools>();


    public ToolSelectComboBox()
    {
      InitializeComponent();

      this.Loaded += new RoutedEventHandler(ToolSelectComboBox_Loaded);
    }

    void ToolSelectComboBox_Loaded(object sender, RoutedEventArgs e)
    {
      // see what checkboxes are checked and update our collection
      UserToolsEnabledCollection_Checked();
    }

    private static void SketchPad_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (e.Property == SketchPadProperty)
      {
        // unsubscribe from old sketch pad
        if (d is ToolSelectComboBox && e.OldValue is SketchPad)
        {
          (e.OldValue as SketchPad).UserCurrentInkToolChanged -=
            new SketchPad.UserCurrentInkToolChangedHandler((d as ToolSelectComboBox).SketchPad_UserCurrentInkToolChanged);

          (e.OldValue as SketchPad).UserToolsEnabledCollectionChanged -=
            new SketchPad.UserToolsEnabledCollectionChangedHandler((d as ToolSelectComboBox).SketchPad_UserToolsEnabledCollectionChanged);
        }

        // subscribe to new sketch pad
        if (d is ToolSelectComboBox && e.NewValue is SketchPad)
        {
          (e.NewValue as SketchPad).UserCurrentInkToolChanged +=
            new SketchPad.UserCurrentInkToolChangedHandler((d as ToolSelectComboBox).SketchPad_UserCurrentInkToolChanged);

          (e.NewValue as SketchPad).UserToolsEnabledCollectionChanged +=
            new SketchPad.UserToolsEnabledCollectionChangedHandler((d as ToolSelectComboBox).SketchPad_UserToolsEnabledCollectionChanged);

          (d as ToolSelectComboBox).SketchPad_UserToolsEnabledCollectionChanged((e.NewValue as SketchPad).UserToolsEnabledCollection);
          (d as ToolSelectComboBox).SketchPad_UserCurrentInkToolChanged((e.NewValue as SketchPad).UserCurrentInkTool);
        }
      }
    }

    private void UserToolsEnabledCollection_Checked(object sender, RoutedEventArgs e)
    {
      // see what checkboxes are checked and update our collection
      UserToolsEnabledCollection_Checked();
    }

    private bool _IsSettingUserToolsEnabledCollection = false;

    private void SketchPad_UserToolsEnabledCollectionChanged(List<SketchPad.InkTools> newInkToolCollection,
      List<SketchPad.InkTools> previousInkToolCollection)
    {
      if (!_IsSettingUserToolsEnabledCollection)
      {
        _IsSettingUserToolsEnabledCollection = true;

        UncheckAll();
        foreach (SketchPad.InkTools inkTool in newInkToolCollection)
        {
          switch (inkTool)
          {
            case SketchPad.InkTools.Pencil:
              Check(_Pencil_Checked);
              break;
            case SketchPad.InkTools.Eraser:
              Check(_Eraser_Checked);
              break;
            case SketchPad.InkTools.Selector:
              Check(_Selector_Checked);
              break;
            case SketchPad.InkTools.Gesture:
              Check(_Gesture_Checked);
              break;
            case SketchPad.InkTools.None:
              Check(_None_Checked);
              break;
            default:
              break;
          }
        }

        _IsSettingUserToolsEnabledCollection = false;
      }
    }

    private void SketchPad_UserToolsEnabledCollectionChanged(List<SketchPad.InkTools> newInkToolCollection)
    {
      if (!_IsSettingUserToolsEnabledCollection)
      {
        _IsSettingUserToolsEnabledCollection = true;
        //UserToolsEnabledCollection = newInkToolCollection;

        UncheckAll();
        foreach (SketchPad.InkTools inkTool in newInkToolCollection)
        {
          switch (inkTool)
          {
            case SketchPad.InkTools.Pencil:
              Check(_Pencil_Checked);
              break;
            case SketchPad.InkTools.Eraser:
              Check(_Eraser_Checked);
              break;
            case SketchPad.InkTools.Selector:
              Check(_Selector_Checked);
              break;
            case SketchPad.InkTools.Gesture:
              Check(_Gesture_Checked);
              break;
            case SketchPad.InkTools.None:
              Check(_None_Checked);
              break;
            default:
              break;
          }
        }

        _IsSettingUserToolsEnabledCollection = false;
      }
    }

    private void UncheckAll()
    {
      Check(_Pencil_Checked, false);
      Check(_Eraser_Checked, false);
      Check(_Selector_Checked, false);
      Check(_Gesture_Checked, false);
      Check(_None_Checked, false);
    }

    private void Check(CheckBox checkBox)
    {
      Check(checkBox, true);
    }

    private void Check(CheckBox checkBox, bool? value)
    {
      if (checkBox != null)
      {
        checkBox.IsChecked = value;
      }
    }

    private void UserToolsEnabledCollection_Checked()
    {
      // see what checkboxes are checked and update our collection
      List<SketchPad.InkTools> userToolsEnabledCollection = new List<SketchPad.InkTools>();

      if (_Selector_Checked != null && _Selector_Checked.IsChecked == true)
      {
        userToolsEnabledCollection.Add(SketchPad.InkTools.Selector);
      }

      if (_Pencil_Checked != null && _Pencil_Checked.IsChecked == true)
      {
        userToolsEnabledCollection.Add(SketchPad.InkTools.Pencil);
      }

      if (_Eraser_Checked != null && _Eraser_Checked.IsChecked == true)
      {
        userToolsEnabledCollection.Add(SketchPad.InkTools.Eraser);
      }

      if (_Gesture_Checked != null && _Gesture_Checked.IsChecked == true)
      {
        userToolsEnabledCollection.Add(SketchPad.InkTools.Gesture);
      }

      if (_None_Checked != null && _None_Checked.IsChecked == true)
      {
        userToolsEnabledCollection.Add(SketchPad.InkTools.None);
      }

      if (!_IsSettingUserCurrentInkTool && SketchPad != null)
      {
        _IsSettingUserCurrentInkTool = true;
        SketchPad.UserToolsEnabledCollection = userToolsEnabledCollection;
        _IsSettingUserCurrentInkTool = false;
      }
    }

    void Pencil_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Pencil;
    }

    void Eraser_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Eraser;
    }

    void Selector_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Selector;
    }

    void Rectangle_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Rectangle;
    }

    void Ellipse_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Ellipse;
    }

    void Image_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Image;
    }

    void Gradient_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Gradient;
    }

    void Text_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Text;
    }

    void Arrow_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Arrow;
    }

    void Line_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Line;
    }

    void Button_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Button;
    }

    void Gesture_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.Gesture;
    }

    void None_Selected(object sender, RoutedEventArgs e)
    {
      UserCurrentInkTool = SketchPad.InkTools.None;
    }

    private bool _IsSettingUserCurrentInkTool = false;

    private static void UserCurrentInkTool_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      if (e.Property == UserCurrentInkToolProperty)
      {
        if (d is ToolSelectComboBox && e.NewValue is SketchPad.InkTools)
        {
          (d as ToolSelectComboBox).SetUserCurrentInkTool((SketchPad.InkTools)e.NewValue);
        }
      }
    }

    

    private void SketchPad_UserCurrentInkToolChanged(SketchPad.InkTools newInkTool, 
      SketchPad.InkTools previousInkTool)
    {
      if (!_IsSettingUserCurrentInkTool)
      {
        _IsSettingUserCurrentInkTool = true;
        UserCurrentInkTool = newInkTool;
        _IsSettingUserCurrentInkTool = false;
      }
    }

    private void SketchPad_UserCurrentInkToolChanged(SketchPad.InkTools newInkTool)
    {
      if (!_IsSettingUserCurrentInkTool)
      {
        _IsSettingUserCurrentInkTool = true;
        UserCurrentInkTool = newInkTool;
        _IsSettingUserCurrentInkTool = false;
      }
    }

    void SetUserCurrentInkTool(SketchPad.InkTools currentInkTool)
    {
      if (!_IsSettingUserCurrentInkTool && SketchPad != null)
      {
        _IsSettingUserCurrentInkTool = true;
        SketchPad.UserCurrentInkTool = UserCurrentInkTool;
        _IsSettingUserCurrentInkTool = false;
      }

      switch (currentInkTool)
      {
        case SketchPad.InkTools.Pencil:
          _Pencil_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Eraser:
          _Eraser_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Selector:
          _Selector_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Rectangle:
          _Rectangle_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Ellipse:
          _Ellipse_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Image:
          _Image_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Gradient:
          _Gradient_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Text:
          _Text_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Arrow:
          _Arrow_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Line:
          _Line_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Button:
          _Button_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.Gesture:
          _Gesture_Selected.IsSelected = true;
          break;
        case SketchPad.InkTools.None:
          _None_Selected.IsSelected = true;
          break;
        default:
          break;
      }
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    #endregion
  }
}